package com.chwn.curiousdiary.ui.register;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.chwn.curiousdiary.DiaryApplication;
import com.chwn.curiousdiary.ui.main.MainActivity;
import com.chwn.curiousdiary.R;
import com.chwn.curiousdiary.api.IUserApi;
import com.chwn.curiousdiary.api.NetWork;
import com.chwn.curiousdiary.gloable.Constant;
import com.chwn.curiousdiary.gloable.CustomEvent;
import com.chwn.curiousdiary.gloable.User;
import com.chwn.curiousdiary.utils.CustomToast;
import com.chwn.curiousdiary.utils.DialogUtil;
import com.chwn.harulib.DatePicker;
import com.chwn.harulib.base.BaseActivity;
import com.chwn.harulib.mvp.rx.HttpResult;
import com.chwn.harulib.util.TimeUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * Created by Myshikin on 2017/6/18.
 */

public class BirthDayActivty extends BaseActivity {

    @BindView(R.id.datePicker)
    DatePicker mDatePicker;

    /**
     * 1--男生 0--女生
     */
    int sex = 1;
    private String phone;
    private long   selectDate;
    private String substring;
    private String unionid;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);  //全屏
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_birthday;
    }

    @Override
    protected void initData() {
        super.initData();
        phone = getIntent().getStringExtra(Constant.PHONE);
        unionid = getIntent().getStringExtra(Constant.UNION_ID);
        sex = getIntent().getIntExtra(Constant.BABYINFO, sex);
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        setBackTVisible(true);
        setBackIcon(R.drawable.icon_back_light);
    }

    @OnClick({R.id.ll_back, R.id.iv_next_step})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.iv_next_step:
                selectDate = TimeUtils.stringTodate(mDatePicker.getSelectDateStr());
                substring = String.valueOf(selectDate).substring(0, 10);
                endAction();
                break;
        }
    }

    private void endAction() {
        if (TextUtils.isEmpty(phone) && DiaryApplication.mInstance.getmUser() != null) {
            phone = DiaryApplication.mInstance.getmUser().getPhone();
        }
        NetWork.getInstance()
                .getApiService(IUserApi.class)
                .upLoadBabyinfo(phone, substring, sex, unionid)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        DialogUtil.showWaitDialog(BirthDayActivty.this, "", false);
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<HttpResult<Object>>() {
                    @Override
                    public void onCompleted() {
                        DialogUtil.dismissWaitDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        DialogUtil.dismissWaitDialog();
                        errAction("");
                    }

                    @Override
                    public void onNext(HttpResult<Object> objectHttpResult) {
                        if (objectHttpResult.getResult() == Constant.RESULT_ERROR) { //失败
                            errAction(objectHttpResult.getErrorMessage());
                            return;
                        }

                        //上传成功 -- 跳转至主界面
                        User user = DiaryApplication.mInstance.getmUser();
                        user.setBabysex(sex + "");
                        user.setBabybirth(substring);

                        DiaryApplication.mInstance.saveUser(user);

                        startActivity(new Intent(BirthDayActivty.this, MainActivity.class));
                        EventBus.getDefault().post(new CustomEvent(CustomEvent.ClOSE_SELF));
                        finish();
                    }
                });
    }

    private void errAction(String errorMsg) {
        String text;
        if (TextUtils.isEmpty(errorMsg)) {
            text = "请检查网络链接";
        } else {
            text = errorMsg;
        }

        CustomToast.showShortToast(text);
    }
}
